<template>
  <div>
    <van-nav-bar title="商品详情页" left-text="返回" left-arrow @click-left="onClickLeft" />
    

    <img :src="productObj.img1" alt="" />
    <div>商品名称:{{ name }}</div>
    <div>商品价格:{{ productObj.originprice }}元</div>
    <div>商品简介:{{ productObj.desc }}</div>

    <van-action-bar>
      <van-action-bar-icon icon="chat-o" text="客服" />
      <van-action-bar-icon icon="shop-o" text="店铺" />
      <van-action-bar-button color="#7232dd" type="warning" text="加入购物车" @click="addProCart"/>
      <van-action-bar-button color="#7232dd" type="danger" text="立即购买" />
    </van-action-bar>
  </div>
</template>
<script>
import { productDetailRequest } from '../api/homeApi.js';
import { addCart } from '@/api/cart.js';

export default {
  data() {
    return {
      productObj: {},
      name: "",
      onClickLeft: ""
    };
  },
  methods: {
    addProCart() {
      if (!localStorage.getItem('userid')) {
        this.$toast('请先登录');
        return;
      }
      const userid = localStorage.getItem('userid');
      console.log(userid);
      const proid = this.$route.query.id;
      console.log(proid)
      const num = 1;
      addCart(userid, proid, num).then(res => {
        console.log(res);
      });
    }
  },
  async mounted() {
    this.name = localStorage.getItem('userid')
    //获取路由传递的参数
    console.log(this.$route.query.id);
    let res = await productDetailRequest(this.$route.query.id);
    console.log(res.data);
    this.productObj = res.data.data;
    this.name = this.productObj.proname;
    this.onClickLeft = () => history.back();
  },
};
</script>
<style lang="css" scoped></style>
